package arrAndstr;

import java.util.HashMap;
import java.util.Map;

/*
    242. 有效的字母异位词
 */
public class T242 {
    public static void main(String[] args) {
        String s = "rat";
        String t = "car";
        System.out.println(isAnagram(s, t));
    }

    public static boolean isAnagram(String s, String t) {
        int lenS = s.length();
        int lenT = s.length();
        if (lenT != lenS)   return false;

        char[] charsS = s.toCharArray();
        char[] charsT = t.toCharArray();
        Map<Character, Integer> map = new HashMap<>();

        for (char ch : charsS) {
            map.put(ch, map.containsKey(ch) ? map.get(ch)+1 : 1);
        }

        for (char ch : charsT) {
            if (!map.containsKey(ch) || map.get(ch)==0) return false;
            map.put(ch, map.get(ch)-1);
        }

        return true;
    }
}
